package com.example.dao;

import com.example.bean.UserBean;
import com.example.mapper.generator.AdminPoMapper;
import com.example.mapper.generator.po.AdminPo;
import com.example.mapper.generator.po.AdminPoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminDao {

    @Autowired
    AdminPoMapper adminPoMapper;

    public UserBean getLoginInfo(Long id, String password) {
        AdminPoExample example = new AdminPoExample();
        AdminPoExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andPasswordEqualTo(password);
        List<AdminPo> adminPoList = adminPoMapper.selectByExample(example);
        if(adminPoList.size() != 1){
            return null;
        } else {
            AdminPo adminPo = adminPoList.get(0);
            return UserBean.builder().id(id).name(adminPo.getName()).password(adminPo.getPassword()).phone(adminPo.getPhone()).email(adminPo.getEmail()).identity("admin").build();
        }
    }

    public Long register(String name, String password, String phone, String email) {
        AdminPo po = AdminPo.builder().name(name).password(password).phone(phone).email(email).build();
        adminPoMapper.insert(po);
        return po.getId();
    }

    public UserBean displayInfo(Long id) {
        AdminPo po = adminPoMapper.selectByPrimaryKey(id);
        return UserBean.builder().id(id).name(po.getName()).password(po.getPassword()).phone(po.getPhone()).email(po.getEmail()).identity("admin").build();
    }
}
